#include "antistress.h"
#include "QSettings"
#include "QDebug"
Antistress::Antistress(QWidget *parent)
    : QMainWindow(parent)
{
	ui.setupUi(this);

        // Retrieve settings
        updateSettings();

	m_home = NULL;
	m_pause = NULL;
	m_work = NULL;
	m_settings = NULL;

        workTimer = new QTimer(this);
        connect(workTimer, SIGNAL(timeout()), this, SLOT(workEnd()));

        pauseTimer = new QTimer(this);
        connect(pauseTimer, SIGNAL(timeout()), this, SLOT(pauseEnd()));

        refreshTimer = new QTimer(this);


        selectHomeWidget();
}

Antistress::~Antistress()
{

}

void Antistress::exit(){
	qApp->exit();
}
void Antistress::selectPauseWidget(){
	if(centralWidget()!=NULL) {
                delete centralWidget();
		m_home = NULL;
		m_pause = NULL;
		m_work = NULL;
		m_settings = NULL;
                refreshTimer->disconnect();
	}
	m_pause = new Pause();

	connect(m_pause,SIGNAL(stopClicked()),this,SLOT(selectHomeWidget()));
        connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshWP()));

        disconnect(this, SIGNAL(refresh(int)), m_pause, SLOT(refresh(int)));
        connect(this, SIGNAL(refresh(int)), m_pause, SLOT(refresh(int)) ,Qt::UniqueConnection);

        sec = 6*lengthPause;
        emit refresh(sec);
        pauseTimer->start(1000* (6*lengthPause));
        refreshTimer->start(1000);

	setCentralWidget(m_pause);
}
void Antistress::selectSettingsWidget() {
	if(centralWidget()!=NULL) {
		delete centralWidget();
		m_home = NULL;
		m_pause = NULL;
		m_work = NULL;
		m_settings = NULL;
                refreshTimer->disconnect();
	}
	m_settings = new Settings();
	connect(m_settings,SIGNAL(backToHomeClicked()),this,SLOT(selectHomeWidget()));
        connect(m_settings,SIGNAL(settingsClicked()),this,SLOT(updateSettings()));

	setCentralWidget(m_settings);

}
void Antistress::selectWorkWidget() {
	if(centralWidget()!=NULL) {
                delete centralWidget();
		m_home = NULL;
		m_pause = NULL;
		m_work = NULL;
                m_settings = NULL;
	}
	m_work = new Work(this);
	connect(m_work,SIGNAL(stopClicked()),this,SLOT(selectHomeWidget()));
        connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshWP()),Qt::UniqueConnection);
        disconnect(this, SIGNAL(refresh(int)), m_work, SLOT(refresh(int)));
        connect(this, SIGNAL(refresh(int)), m_work, SLOT(refresh(int)));

        sec = 3600*freqPauseH + 6*freqPauseMin;
        emit refresh(sec);
        workTimer->start(1000* (freqPauseH*3600 + freqPauseMin*6));
        refreshTimer->start(1000);

	setCentralWidget(m_work);
}
void Antistress::selectHomeWidget() {
	if(centralWidget()!=NULL) {
		delete centralWidget();
		m_home = NULL;
		m_pause = NULL;
		m_work = NULL;
		m_settings = NULL;
	}
	m_home = new Home(this);
	connect(m_home,SIGNAL(startClicked()),this,SLOT(selectWorkWidget()));
	connect(m_home,SIGNAL(exitClicked()),this,SLOT(exit()));
	connect(m_home,SIGNAL(settingsClicked()),this,SLOT(selectSettingsWidget()));

        workTimer->stop();
        pauseTimer->stop();

	setCentralWidget(m_home);
}

void Antistress::updateSettings() {
    QSettings settings("../settings.ini", QSettings::IniFormat);
    freqPauseH = settings.value("freqPauseH",1).toInt();
    freqPauseMin = settings.value("freqPauseMin",30).toInt();
    lengthPause = settings.value("pauseLength",5).toInt();
}

void Antistress::workEnd() {
    qDebug()<< "Work End";
    workTimer->stop();
    selectPauseWidget();
}

void Antistress::pauseEnd() {
    qDebug()<< "Pause End";
    pauseTimer->stop();
    selectWorkWidget();
}

void Antistress::refreshWP() {
    if (sec>0)
        sec--;
    emit refresh(sec);
}

